<?php

/* ******************************
 *	Request aJax actions
* ******************************
*/

require_once('../../includes/classes/core.php');
include('../../includes/classes/log.class.php');
$log 		= new log();
$action 	= $_REQUEST['act'];
$error		= '';
$data		= '';

// Incomming Param
$incom_id				= $_REQUEST['id'];
$call_date				= $_REQUEST['call_date'];
$call_phone				= $_REQUEST['call_phone'];
$category_parent_id		= $_REQUEST['category_parent_id'];
$category_id			= $_REQUEST['category_id'];
$sub_category			= $_REQUEST['sub_category'];
$call_problem_date 	    = $_REQUEST['call_problem_date'];
$call_status_id			= $_REQUEST['call_status_id'];
$call_object_id			= $_REQUEST['call_object_id'];
$call_source_id			= $_REQUEST['call_source_id'];
$call_content			= $_REQUEST['call_content'];

// Person Info Param
$abonent_phone				= $_REQUEST['abonent_phone'];
$abonent_pin				= $_REQUEST['abonent_pin'];
$abonent_name				= $_REQUEST['abonent_name'];
$abonent_responsibl_person	= $_REQUEST['abonent_responsibl_person'];
$abonent_address			= $_REQUEST['abonent_address'];
$abonent_number				= $_REQUEST['abonent_number'];
$abonent_status				= $_REQUEST['abonent_status'];
$abonent_coment				= $_REQUEST['abonent_coment'];

// Task Param
$persons_id			    = $_REQUEST['persons_id'];
$task_type_id			= $_REQUEST['task_type_id'];
$priority_id			= $_REQUEST['priority_id'];
$task_department_id 	= $_REQUEST['task_department_id'];
$edit_id				= $_REQUEST['edit_id'];
$delete_id				= $_REQUEST['delete_id'];

// File Param
$rand_file				= $_REQUEST['rand_file'];
$file					= $_REQUEST['file_name'];
$hidden_inc				= $_REQUEST['hidden_inc'];

switch ($action) {
	case 'get_add_page':
		$number		= $_REQUEST['number'];
		$page		= GetPage($res='', $number);
		$data		= array('page'	=> $page);

		break;
	case 'disable':
		$incom_id				= $_REQUEST['id'];
		mysql_query("			UPDATE `incomming_call`
									    SET `actived`=0
										WHERE `id`=$incom_id ");
		break;
	case 'get_edit_page':
	    $incom_id				= $_REQUEST['id'];
		$page		= GetPage(Getincomming($incom_id));

		$data		= array('page'	=> $page);

		break;
	case 'get_list' :
		$count      = $_REQUEST['count'];
		$hidden     = $_REQUEST['hidden'];
		$user		= $_SESSION['USERID'];

	  	$rResult = mysql_query("SELECT  incomming_call.id,
                                        incomming_call.call_date,
	  	                                IF(ISNULL(task.incomming_call_id),incomming_call.call_phone,CONCAT('<div style=\"background: #71A9D3\">',incomming_call.call_phone,'</div>')),
                                        cat.`name`,
                                        cat_type.`name`,
	  	                                sub_cat.`name`,
                                        object.`name`,
	  	                                call_content,
                                        `persons`.`name`,
	  	                                IF(ISNULL(task.incomming_call_id),1,'<div class=\"imgreen\">0</div>')
                                FROM    incomming_call
                                LEFT JOIN category as cat ON incomming_call.category_parent_id = cat.id
                                LEFT JOIN category as cat_type ON incomming_call.category_id = cat_type.id
	  	                        LEFT JOIN category as sub_cat ON incomming_call.sub_category = sub_cat.id
                                LEFT JOIN object ON incomming_call.call_object_id = object.id
	  	                        LEFT JOIN task ON incomming_call.id = task.incomming_call_id
	  	                        JOIN `users` ON incomming_call.user_id = users.id
                                JOIN `persons` ON users.person_id = persons.id
                                WHERE incomming_call.actived = 1 AND DATE(incomming_call.call_date)=CURDATE() AND incomming_call.user_id = '$user'");
	  
		$data = array(
				"aaData"	=> array()
		);

while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			$row1 = array();
			
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				
				if($i==9){
						 if 	($aRow[$i]=='მოგვარებულია') 			{	$row[] = '<div style="background: #80FF54; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='გარკვევის პროცესშია') 	{	$row[] = '<div style="background:#FFFF55; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='გადაცემულია გასარკვევად') 	{	$row[] = '<div style="background: #FF5555; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 else	{ $row[] = $aRow[$i];}	
				
				}else	{ $row[] = $aRow[$i];}
			}
			$data['aaData'][] = $row;
		}
		
		break;
	case 'save_incomming':
        $incom_id = $_REQUEST['hidden_checker'];
		$task_type_id = $_REQUEST['task_type_id'];
		if($incom_id == ''){
			
            Addincomming( $call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment, $file,$rand_file,$hidden_inc);			
			$incomming_call_id = mysql_insert_id();
			
			if($task_type_id != 0){
			Addtask($incomming_call_id, $persons_id, $task_type_id,  $priority_id, $task_department_id, $call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment);
			}
		
		}else {
			
			Saveincomming($call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment, $file,$rand_file,$hidden_inc);
			
			$check_task = mysql_fetch_assoc(mysql_query("SELECT id FROM task WHERE incomming_call_id = '$incom_id'"));
			
			if($task_type_id != 0){
			    if($check_task == ''){
			         Addtask($incom_id, $persons_id, $task_type_id,  $priority_id, $task_department_id, $call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment);
			    }else{
			         Savetask($check_task[id], $persons_id, $task_type_id,  $priority_id, $task_department_id, $call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment);
			    }
			}
		}
		break;
		
	case 'get_calls':
	
		$data		= array('calls' => getCalls());
	
		break;
		
		case 'delete_file':
		
			mysql_query("DELETE FROM file WHERE id = $delete_id");
		
			$increm = mysql_query("	SELECT  `name`,
					`rand_name`,
					`id`
					FROM 	`file`
					WHERE   `incomming_call_id` = $edit_id
					");
		
			$data1 = '';
		
			while($increm_row = mysql_fetch_assoc($increm))	{
			$data1 .='<tr style="border-bottom: 1px solid #85b1de;">
				          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>
				          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
						          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
 					  </tr>';
		}
		
		$data = array('page' => $data1);
		
				break;
		
				case 'up_now':
				$user		= $_SESSION['USERID'];
				if($rand_file != ''){
				mysql_query("INSERT INTO 	`file`
				( 	`user_id`,
				`incomming_call_id`,
				`name`,
				`rand_name`
				)
				VALUES
				(	'$user',
				'$edit_id',
				'$file',
				'$rand_file'
				);");
				}
		
				$increm = mysql_query("	SELECT  `name`,
				`rand_name`,
				`id`
				FROM 	`file`
				WHERE   `incomming_call_id` = $edit_id
				");
		
				$data1 = '';
		
				while($increm_row = mysql_fetch_assoc($increm))	{
				$data1 .='<tr style="border-bottom: 1px solid #85b1de;">
				<td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>
				<td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
				          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
						          </tr>';
		}
		
		$data = array('page' => $data1);
		
		break;
		
	case 'sub_category':
		
		$cat_id	=	$_REQUEST['cat_id'];
		$data 	= 	array('cat'=>Getcategory1($cat_id));
		
		break;	
	case 'sub_category1':
		
		$cat_id	=	$_REQUEST['cat_id'];
		$data 	= 	array('cat'=>Getcategory2($cat_id));
		
		break;
	case 'task_department':
	
	    $cat_id	=	$_REQUEST['cat_id'];
	    $data 	= 	array('cat'=>Getpersons(''));
	
	    break;			
	case 'set_task_type':
	
		$cat_id	=	$_REQUEST['cat_id'];
		$data 	= 	array('cat'=>Getbank_object($cat_id));
	
		break;
	
	case 'get_add_info':
	
		$pin	=	$_REQUEST['pin'];
		$data 	= 	array('info' => get_addition_all_info($pin));
	
		break;
		case 'get_add_info1':
		
		$personal_id	=	$_REQUEST['personal_id'];
		$data 	= 	array('info1' => get_addition_all_info1($personal_id));
	
		break;
	default:
		$error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Request Functions
* ******************************
*/

function Addincomming( $call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment, $file,$rand_file,$hidden_inc)
{
	
	$call_date				= date('Y-m-d H:i:s');
	$user					= $_SESSION['USERID'];
	
	mysql_query("INSERT INTO `incomming_call` 
(`user_id`,`abonent_phone`,`abonent_pin`,`abonent_name`,`abonent_responsibl_person`,`abonent_address`,`abonent_number`,`abonent_status`,`abonent_coment`,`call_date`,`call_phone`,`category_parent_id`,`category_id`,`sub_category`,`call_problem_date`,`call_status_id`,`call_object_id`,`call_source_id`,`call_content`,`actived`) 
VALUES 
('$user','$abonent_phone','$abonent_pin','$abonent_name','$abonent_responsibl_person','$abonent_address','$abonent_number','$abonent_status','$abonent_coment','$call_date','$call_phone','$category_parent_id','$category_id','$sub_category','$call_problem_date','$call_status_id','$call_object_id','$call_source_id','$call_content','1')");
	
	if($rand_file != ''){
		mysql_query("INSERT INTO 	`file`
		( 	`user_id`,
		`incomming_call_id`,
		`name`,
		`rand_name`
		)
		VALUES
		(	'$user',
		'$hidden_inc',
		'$file',
		'$rand_file'
		);");
	}
}

function Addtask($incomming_call_id, $persons_id, $task_type_id,  $priority_id, $task_department_id, $call_date,   $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment)
{
	
	$user		= $_SESSION['USERID'];
	mysql_query("INSERT INTO	`task` 
									(`user_id`,
									 `responsible_user_id`,
									 `date`,
									 `incomming_call_id`,
									 `task_type_id`,
									 `priority_id`,
									 `department_id`,
									 `phone`,
									 `call_type_id`,
									 `category_id`,
									 `subcategory_id`,
									 `object_id`,
									 `problem_date`,
									 `task_source_id`,
									 `comment`,
									 `abonent_phone`,
									 `abonent_pin`,
									 `abonent_name`,
									 `abonent_responsibl_person`,
									 `abonent_address`,
									 `abonent_number`,
									 `abonent_status`,
									 `abonent_coment`
									 )
									
						VALUES
									('$user',
									 '$persons_id',
									 '$call_date',
									 '$incomming_call_id',
									 '$task_type_id',
									 '$priority_id',
								     '$task_department_id',
								     '$call_phone',
								     '$category_parent_id',
								     '$category_id',
								     '$sub_category',
								     '$call_object_id',
								     '$call_problem_date',
								     '$call_source_id',
								     '$call_content',
								     '$abonent_phone',
								     '$abonent_pin',
								     '$abonent_name',
								     '$abonent_responsibl_person',
								     '$abonent_address',
								     '$abonent_number',
								     '$abonent_status',
								     '$abonent_coment'
								  	  )");
								   
}

				
function Saveincomming($call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment, $file,$rand_file,$hidden_inc)
{

	$incom_id	= $_REQUEST['id'];
	$user		= $_SESSION['USERID'];
	$hidden_user	= $_REQUEST['hidden_user'];

		mysql_query("UPDATE `incomming_call` SET 
                            `user_id`='$user', 
                            `abonent_phone`='$abonent_phone', 
                            `abonent_pin`='$abonent_pin', 
                            `abonent_name`='$abonent_name', 
                            `abonent_responsibl_person`='$abonent_responsibl_person', 
                            `abonent_address`='$abonent_address', 
                            `abonent_number`='$abonent_number', 
                            `abonent_status`='$abonent_status', 
                            `abonent_coment`='$abonent_coment', 
                            `call_date`='$call_date', 
                            `call_phone`='$call_phone', 
                            `category_parent_id`='$category_parent_id', 
                            `category_id`='$category_id', 
                            `sub_category`='$sub_category', 
                            `call_problem_date`='$call_problem_date', 
                            `call_status_id`='$call_status_id', 
                            `call_object_id`='$call_object_id', 
                            `call_source_id`='$call_source_id', 
                            `call_content`='$call_content'
                    WHERE   `id`='$incom_id'");					
	
	
}      
 
function Savetask($check_task, $persons_id, $task_type_id,  $priority_id, $task_department_id, $call_date,  $call_phone, $category_parent_id, $category_id, $sub_category, $call_problem_date, $call_status_id, $call_object_id, $call_source_id, $call_content, $abonent_phone, $abonent_pin, $abonent_name, $abonent_responsibl_person, $abonent_address, $abonent_number, $abonent_status, $abonent_coment)
{


	$user  = $_SESSION['USERID'];
	
			mysql_query("UPDATE  `task` SET  	 
    							 `user_id`='$user',
    						 	 `responsible_user_id`='$persons_id',
    						 	 `task_type_id`='$task_type_id',
    							 `priority_id`='$priority_id', 
    							 `department_id`='$task_department_id',
    							 `phone`='$call_phone',
								 `call_type_id`='$category_parent_id',
								 `category_id`='$category_id',
								 `subcategory_id`='$sub_category',
								 `object_id`='$call_object_id',
								 `problem_date`='$call_problem_date',
								 `task_source_id`='$call_source_id',
								 `comment`='$call_content',
								 `abonent_phone`='$abonent_phone', 
                                `abonent_pin`='$abonent_pin', 
                                `abonent_name`='$abonent_name', 
                                `abonent_responsibl_person`='$abonent_responsibl_person', 
                                `abonent_address`='$abonent_address', 
                                `abonent_number`='$abonent_number', 
                                `abonent_status`='$abonent_status', 
                                `abonent_coment`='$abonent_coment' 
					     WHERE   (`id`='$check_task');");

}



function Getcall_status($status)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `call_status`
						FROM 	`status`
						WHERE 	actived=1");
	

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		
		if($res['id'] == $status){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['call_status'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['call_status'] . '</option>';
		}
	}
	return $data;
}

function Getobject($object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`object`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getsource( $object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`source`
					WHERE 	actived=1");


	if ($object_id=='') {
	    $object_id=1;
	}
	
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getcategory($category_id)
{

    $data = '';
    $req = mysql_query("SELECT `id`, `name`
        FROM `category`
        WHERE actived=1 && parent_id = 0");

    $data .= '<option value="-" selected="selected">----</option>';
    while( $res = mysql_fetch_assoc($req)){
        if($res['id'] == $category_id){
            $data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
        } else {
            $data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
        }
    }
    return $data;
}

function Getcategory1($category_id,$id)
{

    $data = '';
    $req = mysql_query("SELECT `id`, `name`
        FROM `category`
        WHERE actived=1 && parent_id = $category_id");

    $data .= '<option value="-" selected="selected">----</option>';
    while( $res = mysql_fetch_assoc($req)){
        if($res['id'] == $id){
            $data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
        } else {
            $data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
        }
    }
    return $data;
}

function Getcategory2($category_id,$id)
{

    $data = '';
    $req = mysql_query("SELECT `id`, `name`
        FROM `category`
        WHERE actived=1 && parent_id = $category_id");

    $data .= '<option value="-" selected="selected">----</option>';
    while( $res = mysql_fetch_assoc($req)){
        if($res['id'] == $id){
            $data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
        } else {
            $data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
        }
    }
    return $data;
}

function Getdepartment($task_department_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `department`
					    WHERE actived=1 ");
	

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_department_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getpriority($priority_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `priority`
						WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $priority_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Gettask_type($task_type_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `task_type`
					    WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getpersons($persons_id)
{
	$data = '';
	$req = mysql_query("SELECT users.`id`, 
	                           persons.`name`
    					FROM   `persons`
    					JOIN   users ON persons.id = users.person_id
    					WHERE  persons.actived=1 AND users.id != 1");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $persons_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function getCalls(){
	
$req = mysql_query("

						SELECT  	DISTINCT
									IF(SUBSTR(cdr.src, 1, 3) = 995, SUBSTR(cdr.src, 4, 9), cdr.src) AS `src`
						FROM    	cdr
						GROUP BY 	cdr.src
						ORDER BY 	cdr.calldate DESC
						LIMIT 		12


						");

	$data = '<tr class="trClass">
					<th class="thClass">#</th>
					<th class="thClass">ნომერი</th>
					<th class="thClass">ქმედება</th>
				</tr>
			';
	$i	= 1;
	while( $res3 = mysql_fetch_assoc($req)){

		$data .= '
	    		<tr class="trClass">
					<td class="tdClass">' . $i . '</td>
					<td class="tdClass" style="width: 30px !important;">' . $res3['src'] . '</td>
					<td class="tdClass" style="font-size: 13px !important;"><button class="insert" number="' . $res3['src'] . '">დამატება</button></td>
				</tr>';
		$i++;
	}

	return $data;


}

function get_addition_all_info($pin)
{
	$res1 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.pin = $pin AND incomming_call.call_type_id = 1 AND (task.`status` = 3 OR ISNULL(task.`status`))"));
	
	$res2 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a1`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.pin = $pin AND incomming_call.call_type_id = 2 AND (task.`status` = 3 OR ISNULL(task.`status`))"));
	
	$res3 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a2`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.pin = $pin AND incomming_call.call_type_id = 1 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));
	
	$res4 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a3`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.pin = $pin AND incomming_call.call_type_id = 2 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));
	
	$sum_incomming	 	= $res1['a'] + $res2['a1'] + $res3['a2'] + $res4['a3'];
	$sum_end	 		= $res1['a'] + $res2['a1'];
	$sum_actived		= $res3['a2'] + $res4['a3'];
	
	$data .= '<fieldset>
					<legend>მომართვების ისტორია</legend>
					<table>
						<tr>
							<td>სულ</td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_incomming.'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოგვარებულები</td>
							<td></td>
							<td style="width: 150px;"></td>
						    <td>'.$sum_end.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრეტენზია</td>
							<td style="width: 150px;"></td>
							<td>'. $res1['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'.$res2['a1'].'</td>
						</tr>
						<tr>
							<td></td>
							<td>პრეტენზია</td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_actived.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრეტენზია</td>
							<td style="width: 150px;"></td>
							<td>'.$res3['a2'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'. $res4['a3'].'</td>
						</tr>
					</table>
				</fieldset>';

	return $data;
}
function get_addition_all_info1($personal_id)
{
	$res1 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.personal_id = $personal_id AND incomming_call.call_type_id = 1 AND (task.`status` = 3 OR ISNULL(task.`status`))"));

	$res2 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.personal_id = $personal_id AND incomming_call.call_type_id = 2 AND (task.`status` = 3 OR ISNULL(task.`status`))"));

	$res3 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.personal_id = $personal_id AND incomming_call.call_type_id = 1 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));

	$res4 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`site_user`
											JOIN 			incomming_call ON site_user.incomming_call_id = incomming_call.id
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			site_user.personal_id = $personal_id AND incomming_call.call_type_id = 2 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));

	$sum_incomming	 	= $res1['a'] + $res2['a'] + $res3['a'] + $res4['a'];
	$sum_end	 		= $res1['a'] + $res2['a'];
	$sum_actived		= $res3['a'] + $res4['a'];

	$data .= '<fieldset>
					<legend>მომართვების ისტორია</legend>
					<table>
						<tr>
							<td>სულ</td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_incomming.'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოგვარებულები</td>
							<td></td>
							<td style="width: 150px;"></td>
						    <td>'.$sum_end.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრეტენზია</td>
							<td style="width: 150px;"></td>
							<td>'. $res1['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'.$res2['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td>პრეტენზია</td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_actived.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრეტენზია</td>
							<td style="width: 150px;"></td>
							<td>'.$res3['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'. $res4['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
				    		<td style="width: 300px;">
							<input type="button" value="ვრცლად"/>
		      				</td>
						</tr>
					</table>
				</fieldset>';

	return $data;
}
function Getincomming($incom_id)
{
$res = mysql_fetch_assoc(mysql_query("	SELECT 	id,
                                                user_id,
                                				abonent_address,
                                				abonent_coment,
                                				abonent_name,
                                				abonent_number,
                                				abonent_phone,
                                				abonent_pin,
                                				abonent_responsibl_person,
                                				abonent_status,
                                				call_content,
                                				call_date,
                                				call_object_id,
                                				call_phone,
                                				call_problem_date,
                                				call_source_id,
                                				call_status_id,
                                				category_id,
                                				category_parent_id,
                                				sub_category
                                        FROM	incomming_call 
                                        WHERE   id = '$incom_id'
			" ));
	
	return $res;
}


function GetPage($res='', $number)
{
	$num = 0;
	if($res[phone]==""){
		$num=$number;
	}else{ 
		$num=$res[phone]; 
	}
	if ($res[id]!='') {
		
		$dis='disabled="disabled"';
	}else{
		$dis='';
	}
	if($res['category_parent_id'] != ''){
	    $cat_ch = $res['category_parent_id'];
	}

	$increm = mysql_query("	SELECT  `name`,
									`rand_name`
							FROM 	`file`
							WHERE   `incomming_call_id` = $res[id]
			");
	
	$call_date				= date('Y-m-d H:i:s');
	
	$data  .= '
	<!-- jQuery Dialog -->
    <div id="add-edit-goods-form" title="საქონელი">
    	<!-- aJax -->
	</div>
	<div id="dialog-form">
			<div style="float: left; width: 673px;">	
				<fieldset >
			    	<legend>ძირითადი ინფორმაცია</legend>
		
			    	<table width="100%" class="dialog-form-table">
						<tr>
							<td style="width: 180px;"><label for="id">მომართვა №</label></td>
							<td style="width: 180px !important;"><label for="call_date">თარიღი</label></td>
							<td style="width: 180px;"><label for="call_phone">ტელეფონი</label></td>
						</tr>
						<tr>
							<td style="width: 180px;">
								<input type="text" id="id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField user_id\'" value="' . (($res['id']!='')?$res['id']:increment("incomming_call")). '" disabled="disabled" />
							</td>
							<td style="width: 180px;">
								<input type="text" id="call_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField date\'" value="' . (($res['call_date']!="")? $res['call_date'] : $call_date) . '" disabled="disabled" />
							</td>
							<td style="width: 180px;">
								<input type="text" id="call_phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . (($res['call_phone']!="")? $res['call_phone'] : $num) . '" />
							</td>
							<td style="width: 69px;">
								<button id="button_calls" class="calls">ნომრები</button>
							</td>
						</tr>
						<tr>
							<td style="width: 180px;"><label for="category_parent_id">კატეგორია</label></td>
							<td style="width: 180px;"><label for="category_id">ზარის ტიპი</label></td>
							<td colspan="2"><label for="sub_category">ქვე-კატეგოტია</label></td>
							
						</tr>
						<tr>
							<td style="width: 180px;"><select id="category_parent_id" class="idls object">'. Getcategory($res['category_parent_id']).'</select></td>
							<td style="width: 180px;"><select id="category_id" class="idls object">'. Getcategory1($res['category_parent_id'],$res['category_id']).'</select></td>
							<td colspan="2"><select id="sub_category" style="width: 258px;" class="idls object">'. Getcategory2($res['category_id'],$res['sub_category']).'</select>  </td>
							
						</tr>
					</table>

		    <table width="100%" class="dialog-form-table">
						<tr>
							<td style="width: 183px;"><label for="call_problem_date">პრობლემის თარიღი</label></td>
							<td style="width: 184px;"><label for="call_status">ინფორმაციის წყარო</label></td>
							<td style="width: 180px;"><label for="call_object_id">ბიზნეს ცენტრი</label></td>						
						</tr>
						<tr>	
							<td> <input type="text" id="call_problem_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['call_problem_date'] . '" /></td>
							<td style="width: 180px;"><select id="call_source_id" class="idls object">'. Getsource($res['call_source_id']).'</select></td>
							<td colspan="2"><select  style="width: 258px;" id="call_object_id" class="idls object">'. Getobject($res['call_object_id']).'</select></td>						
						</tr>
						<tr>
							<td style="width: 180px;"><label for="call_content">საუბრის შინაარსი</label></td>
						</tr>
						<tr>
							
							<td colspan="6">	
								<textarea  style="width: 641px; resize: none;" id="call_content" class="idle" name="call_content" cols="300" rows="4">' . $res['call_content'] . '</textarea>
							</td>
						</tr>			
					</table>
				</fieldset >
<!-------------------------------------------------------------------დავალება-------------------------------------------------------------------------->			   
				<fieldset style="margin-top: 5px;">
			    	<legend>დავალების ფორმირება</legend>
		
			    	<table class="dialog-form-table">
						<tr>
							<td style="width: 180px;"><label for="d_number">დავალების ტიპი</label></td>
							<td ><label for="d_number">განყოფილება</label></td>
							
						</tr>
			    		<tr>
							<td ><select  id="task_type_id" class="idls object">'.Gettask_type($res['task_type_id']).'</select></td>
							<td ><select style="width: 465px;" id="task_department_id" class="idls object">'. Getdepartment($res['task_department_id']).'</select></td>
						</tr>
					</table>		    
					<table class="dialog-form-table">
							    		    
						<tr>
							<td style="width: 500px;"><label for="d_number">პასუხისმგებელი პირი</label</td>	    
							<td style="width: 180px;"><label for="d_number">პრიორიტეტები</label></td>													
						</tr>
						<tr>
							<td ><select  style="width: 450px;" id="persons_id" class="idls object">'.Getpersons($res['persons_id']).'</select></td>    
							<td style="width: 180px;"><select id="priority_id" class="idls object">'.Getpriority($res['priority_id']).'</select></td>
						</tr>
					</table>
		        </fieldset>
			</div>
			<div>
				  </fieldset>
			</div>
			<div style="float: right;  width: 355px;">
					    
<!---------------------------------------------------------------კლიენტი---------------------------------------------------------------------------->							    
				 <fieldset onload="legal()">
					<legend>მომართვის ავტორი</legend>
					<table style="height: 172px;">
						<tr style="height:20px;">
							<td style="padding: 0px 0px 10px 45px;"><input type="radio" style="float:left;" class="legal" onclick="legal()" value="fhisical" name="legal" ' . (($res['abonent_status']=="fhisical")? "checked" : "checked") . '><span style="display: inline-block; margin: 8px;" >ფიზიკური </span></td>
							<td style="width: 180px; height:20px;"><input type="radio" style="float:left;" class="legal" onclick="legal()" value="Jural" name="legal" ' . (($res['abonent_status']=="Jural")? "checked" : "") . '><span style="display: inline-block; margin: 8px;">იურიდიული </td>
						</tr>
						<tr style="height:20px;">
							<td style="width: 180px; height:20px;">ტელეფონი</td>
							<td style="width: 180px; height:20px;"><div class="fhisical">პირადი ნომერი</div><div class="Jural">საიდენთიპიკაციო კოდი</div></td>
						</tr>	    
						<tr style="height:40px;">
							<td style="width: 180px; height:20px;"><input type="text" id="abonent_phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['abonent_phone']  . '" /></td>
                            <td style="width: 180px; height:20px;"><input type="text" id="abonent_pin" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['abonent_pin']  . '" /></td>
						</tr>
						<tr style="height:20px;">
							<td colspan="2" style="height:20px;"><div class="fhisical">აბონენტის სახელი გვარი</div><div class="Jural">ორგანიზაცია</div></td>
						</tr>
						<tr style="height:40px;">
							<td colspan="2" style="height:20px;">
								<input type="text" id="abonent_name" style="width: 328px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['abonent_name'] . '" />
							</td>		
						</tr>
						<tr style="height:20px;" class="Jural">
							<td colspan="2" style="height:20px;">პასუხისმგებელი პირი</td>
						</tr>		    
						<tr style="height:40px;" class="Jural">
							<td colspan="2" style="height:20px;">
								<input type="text" id="abonent_responsibl_person" style="width: 328px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['abonent_responsibl_person'] . '" />
							</td>		
						</tr>		    
						<tr style="height:20px;">
							<td colspan="2" style="height:20px;">მისამართი</td>
						</tr>
						<tr style="height:20px;">
							<td colspan="2" style="height:40px;">
								<input type="text" id="abonent_address" style="width: 328px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['abonent_address'] . '" />
							</td>		
						</tr>
						<tr style="height:20px;">
							<td style="width: 180px;"></td>
							<td style="width: 180px;">აბონენტის ნომერი</td>
						</tr>	    
						<tr style="height:40px;">
							<td style="width: 180px;">შენიშვნა</td>
                            <td style="width: 180px;"><input type="text" id="abonent_number" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['abonent_number']  . '" /></td>
						</tr>
                        <tr style="height:40px;">
							<td colspan="2">
								<textarea type="text" id="abonent_coment" style="width: 328px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" >' . $res['abonent_coment'] . '</textarea>
							</td>		
						</tr>        		    		    
						
					</table>
				</fieldset>
				';
					
	  		$data .= GetRecordingsSection($res);
	  		
		$data .= '		<fieldset>
					<legend>დამატებითი ინფორმაცია</legend> 
					<table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
					<tr>
						<td>
							<div class="file-uploader">
								<input id="choose_file" type="file" name="choose_file" class="input" style="display: none;">
								<button id="choose_button" class="center">აირჩიეთ ფაილი</button>
								<input id="hidden_inc" type="text" value="'. increment('incomming_call') .'" style="display: none;">
							</div>
						</td>
					</tr>
				</table>
			     <table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
			          <tr style="border-bottom: 1px solid #85b1de;">
			           <td colspan="3">მიმაგრებული ფაილი</td>
			          </tr>
				</table>
				<table id="file_div" style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">';
					
					while($increm_row = mysql_fetch_assoc($increm))	{	
						$data .=' 
								        <tr style="border-bottom: 1px solid #85b1de;">
								          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>													 
								          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
								          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
								        </tr>';
					}
			         
	 $data .= '
	 		</table>
				</fieldset>
			</div>
	 		<input id="hidden_checker" type="text" value="'. $res['id'] .'" style="display: none;">
    </div>';

	return $data;
}

function GetRecordingsSection($res)
{
	if ($res[call_phone]==''){
		//$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}else{
	$req = mysql_query("SELECT  	DATE_FORMAT(asterisk_incomming.call_datetime,'%d/%m/%y %H:%i:%s') AS `time`,
									IF(DATE(call_datetime) < '2015-04-01',concat(DATE_FORMAT(asterisk_incomming.call_datetime, '%Y/%m/'),`asterisk_incomming`.`file_name`),concat(DATE_FORMAT(asterisk_incomming.call_datetime, '%Y/%m/%d/'),`asterisk_incomming`.`file_name`)) AS `userfield`
                        FROM 		asterisk_incomming
                        WHERE		asterisk_incomming.source = $res[call_phone] AND disconnect_cause != 'ABANDON'");
	}
	$data .= '
        <fieldset style="margin-top: 10px; width: 333px; float: right;">
            <legend>ჩანაწერები</legend>

            <table style="width: 65%; border: solid 1px #85b1de; margin:auto;">
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <th style="padding-left: 10px;">დრო</th>
                    <th  style="border: solid 1px #85b1de; padding-left: 10px;">ჩანაწერი</th>
                </tr>';
	if (mysql_num_rows($req) == 0){
		$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}
	
	while( $res2 = mysql_fetch_assoc($req)){
		$src = $res2['userfield'];
		$data .= '
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <td>' . $res2['time'] . '</td>
                    <td><button class="download" str="' . $src . '">მოსმენა</button></td>
                </tr>';
	}

	$data .= '
            </table>
        </fieldset>';
	
	return $data;
}
function increment($table){

	$result   		= mysql_query("SHOW TABLE STATUS LIKE '$table'");
	$row   			= mysql_fetch_array($result);
	$increment   	= $row['Auto_increment'];
	$next_increment = $increment+1;
	mysql_query("ALTER TABLE '$table' AUTO_INCREMENT=$next_increment");

	return $increment;
}

?>